package com.qdxwx.manage.controller.document;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.config.UploadConfig;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.FileUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.approal.ApproalService;
import com.qdxwx.data.service.document.DocumentService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.approal.Approal;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.document.DocumentDetail;
import com.qdxwx.models.enums.ApprovalStatus;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.manager.Manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 文库审核操作 Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/document")
public class DocumentApprovalController extends BaseController {

    @Autowired
    private DocumentService documentService;
    @Autowired
    private ApproalService approalService;

    @Permissions("document:approval")
    @GetMapping("/approval.html")
    public String approval(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        int status = TKConvert.toInteger(request.getParameter("status"));
        if (id < 1) return error404;
        if (status == 0 || status > 1 || status < -1) return error404;
        Document document = documentService.get(id);
        if (document == null) return error404;
        mmap.put("document", document);
        mmap.put("status", status);
        return "document/document-approval";
    }

    @Permissions("document:approval")
    @PostMapping("/approval.json")
    @Log(title = "编辑审核操作", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult approval(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if(manager == null) return error("登录超时");
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error("缺少参数id");
        DocumentDetail document = documentService.get(id);
        if (document == null) return error("找不到指定id的资料");
        if (!document.getStatus().equals(0)) return error("审核过的资料，无需再审");
        int status = TKConvert.toInteger(request.getParameter("status"));
        if (status == 0 || status > 1 || status < -1) return error("参数状态错误");
        String comment = TKString.trim(request.getParameter("comment"));
        if(TKString.isEmpty(comment)) return error("请输入审核描述");
        document.setPrice(TKConvert.toInteger(request.getParameter("price")) * 100);
        document.setStatus(status);
        document.setCensorId(manager.getManagerId());
        document.setCensorName(manager.getManagerName());
        document.setCensorTime(LocalDateTime.now());
        //已审核状态文件修改
        if (document.getStatus().equals(ApprovalStatus.PASSED.getValue())) {
            //判断文件有没有修改
            if (!TKString.isEmpty(document.getFileUrl())) {
                //原
                String oldPath = document.getFileUrl();
                //新
                if (oldPath.contains("/temps/")) {
                    String newPath = String.format("%s/%s", UploadConfig.uploadFileFolder, oldPath.substring(oldPath.indexOf("/temps/") + 7));
                    if (!FileUtils.copyFile(UploadConfig.uploadRoot + oldPath, UploadConfig.uploadRoot + newPath)) {
                        logger.error("复制文件失败：" + oldPath);
                        return error("文件保存失败,请联系管理员");
                    }
                    document.setFileUrl(newPath);
                }
            }
        }
        int row = documentService.update(document);
        if(row > 0){
            Approal approal = new Approal();
            approal.setInfoId(document.getId());
            approal.setTypeId(ModuleType.DOCUMENT.ordinal());
            approal.setStatus(status);
            approal.setComment(comment);
            approal.setManageId(manager.getManagerId());
            approal.setManageName(manager.getManagerName());
            approalService.insert(approal);
        }
        return toAjax(row);
    }
}
